<?
require_once('../bootstrap.php');
require_once('../includes/site_config.php');

auth_redirect();
$author = get_author();

$files = array();
$folder_path = '../' . FILES_FOLDER;
if (is_dir($folder_path)) {
	$enties = scandir($folder_path);
	foreach ($enties as $entry) {
		$entry_path = $folder_path . '/' . $entry;
		if (is_file($entry_path)) {
			$files[] = $entry;
		}
	}
}

if (isset($_POST['submit'])) {
	if (isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
		$success = false;		
		if (in_array($_FILES['file']['type'], $GLOBALS['ALLOWED_MIME_TYPES'])) {
			$destination = '../' . FILES_FOLDER . '/' . $_FILES['file']['name'];
			if (move_uploaded_file($_FILES['file']['tmp_name'], $destination)) {
				set_flash('Filen blev uploaded.', $PHP_SELF);
			}
		}
		set_flash('Filen kunne ikke uploades.', $PHP_SELF);
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>KomitCMS | Filer	</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/style.css" />
		<script src="../javascript/prototype.js" type="text/javascript"></script>
		<script type="text/javascript">
			
			function disableSubmit() {
				$('submit').disabled = 'disabled';
			}
			
			function showLoading() {
				$('upload_form_placeholder').style.display = 'none';
				$('upload_loader').style.display = 'block';
			}
			
			function uploadImage() {
				disableSubmit();
				showLoading();
			}
			
		</script>
	</head>
	<body>
		<div class="AUI_Header">
			<h1>Admin</h1>
			<? include 'menu.php' ?>
		</div>
		
		<div class="AUI_Content">
			<h2>Filer</h2>
			<p>Her kan du uploade indhold til dine sider i form af billeder og lignende.<br/>
				Nedenunder er en liste med filer du allerede har uploaded.</p>
			
			<? if (($m = get_flash()) != null) { ?>
				<div class="UI_Notice_OK">
					<?= $m ?>
				</div>
			<? } ?>
			
			<? if (count($files)) { ?>
				<ul>
				<? foreach ($files as $file) { ?>
					<li><span><a href="<?= $folder_path . '/' . $file ?>">
						<?= $file ?> (files/<?= $file ?>)</a>
					</span></li>
				<? } ?>
				</ul>
			<? } ?>
			
			<br/><br/>
			<div id="upload_form_placeholder" class="AUI_UploadFileForm">
				<form action="<?= $PHP_SELF ?>" method="post" enctype="multipart/form-data">
					
					<span class="AUI_UploadElement">
						<input type="file" id="file" name="file" />
					</span>
					
					<input type="submit" name="submit" id="submit" value="Upload" />
					
				</form>
			</div>
			
			<div id="upload_loader" class="AUI_UploadLoading hidden">
				<img src="../images/ajax-loader-3.gif" alt="" />
			</div>
		</div>
	</body>
</html>